package com.cn.sku.aspect;

import com.cn.buss.LzlCompany;
import com.cn.dataSource.Constants;
import com.cn.dataSource.DynamicDataSourceContxtHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Order(2)
public class OpenAspect {

    @Pointcut("execution(public * com.cn.sku.service.open..*.*(com.cn.buss.LzlCompany,..)))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object process(ProceedingJoinPoint point) throws Throwable {
        // 访问目标方法的参数：
        Object[] args = point.getArgs();
        LzlCompany company = (LzlCompany) args[0];
        if (Constants.DS_NAME_LIST.contains(company.getDataSource())) {
            DynamicDataSourceContxtHolder.setDataSourceType(company.getDataSource());
        }
        try {
            return point.proceed();
        } finally {
            // 销毁 在执行方法之后
            DynamicDataSourceContxtHolder.clearDataSourceType();
        }

    }

}
